const Controller = require('egg').Controller

class HomeController extends Controller {
  async index() {
    this.ctx.body = `hi, egg-RESTfulAPI!
    A optimized Node.js RESTful API Server Template based on egg.js.
    https://github.com/icxcat/egg-RESTfulAPI.git`
  }

  async init() {
    const { ctx, service } = this

    const rolejson = {
      name: 'admin',
      access: 'admin'
    }

    const options = { upsert: true, new: true, setDefaultsOnInsert: true }
    const role = await ctx.model.Role.findOneAndUpdate(rolejson, rolejson, options)
    // const role = await ctx.model.Role.findOne(rolejson) || await service.role.create(rolejson)

    // console.log(role)
    // 组装参数
    const user = {
      mobile: '13988889999',
      realName: 'admin',
      role: role
    }

    const newUser = Object.assign(user, { password: '123123' })
    // 调用 Service 进行业务处理
    // const res = await ctx.model.User.findOne(user) || await service.user.create(Object.assign(user, { password: '123123' }))
    const res = await ctx.model.User.findOneAndUpdate(user, newUser, options)

    // 设置响应内容和响应状态码
    // ctx.helper.success({ ctx })
    ctx.helper.success({ ctx, res })
  }


  async echo() {
    const { ctx } = this
    // const _id = ctx.state.user && ctx.state.user.data._id // GET ID
    const post = ctx.request.body || {} // POST
    const get = ctx.query // GET

    console.log(post, get)

    const res = {
      post,
      get
    }

    ctx.helper.success({ ctx, res })
  }

  // https://dcache.iqiyi.com/api/flow?deviceid=F5E1D8B7F758A37C&endtime=1597587593&starttime=1597505193

  async getFlows() {
    const { ctx } = this
    const post = ctx.request.body || {} // POST
    const get = ctx.query // GET

    // console.log( post , get)

    const { deviceid } = get
    const device = await ctx.model.Devices.findOne({ deviceid })
    if (device) {

      const now = new Date()
      // now.setHours(now.getHours() - 24)
      const end = parseInt(now.getTime() / 1000)
      // console.log(now)
      now.setHours(now.getHours() - 6)
      // console.log(now)
      const start = parseInt(now.getTime() / 1000)

      const url = `https://dcache.iqiyi.com/api/flow?deviceid=${deviceid}&endtime=${end}&starttime=${start}`

      // console.log(url)
      // https://dcache.iqiyi.com/api/flow?deviceid=F5E1D8B7F758A37C&endtime=1597587593&starttime=1597505193
      const list = await ctx.curl(url, {
        dataType: 'json',
        timeout: 3000,
        // 参数必须按照 `user:password` 格式设置
        auth: 'ppio-pi:Pplive123',
      })

      // let count = 0

      // console.log(list.data.data)
      const options = { upsert: true, new: true, setDefaultsOnInsert: true }
      if (list.data && list.data.data) {
        for (const item of list.data.data) {

          const query = { device: device._id, timestamp: item.timestamp }
          item.device = device._id
          // console.log(query, item)

          const res = await ctx.model.Flows.findOneAndUpdate(query, item, options).exec()
          // console.log(res)
        }
      }
      ctx.helper.success({ ctx, res: 'OK' })
    } else {

      const res = 'devices not found'

      ctx.helper.success({ ctx, res })
    }


  }

}

module.exports = HomeController
